package org.example.leetcode;

/**
 * @author: lynn
 * @Descript:最长回文子串
 * @date: 2021/7/12 14:50
 * @version: 1.0
 */
public class LC5 {

    public static void main(String[] args) {
        String s="xadeeday";
        System.out.println(longestPalindrome(s));
    }

    //fixme 中心扩散法
    public static String longestPalindrome(String s) {
         int start=0;
         int index=0;
         int max=0;
         while (index<s.length()){
             int tmp=Math.max(longestPalindromeHelper(s,index,index),longestPalindromeHelper(s,index,index+1));
             if (tmp>max){
                 max=tmp;
                 start=index-(tmp+1)/2+1;
             }
             index++;
         }
        //fixme "abcdeft".substring(1,4)--> bcd 代表从0开始，前闭后开
        return s.substring(start,start+max);
    }

    //获取最长不重复的长度
    private static int longestPalindromeHelper(String s,int l,int r){
        while (l>=0 && r<s.length() && s.charAt(l)==s.charAt(r)){
            l--;
            r++;
        }
        return r-l-1;
    }
}
